package com.wanmait.happyheating.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wanmait.happyheating.pojo.District;
import com.wanmait.happyheating.pojo.Workers;
import com.wanmait.happyheating.service.DistrictService;
import com.wanmait.happyheating.service.WorkersService;
import com.wanmait.happyheating.util.PictureUtils;
import com.wanmait.happyheating.util.Result;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.UUID;

/**管理员添加员工的控制器
 * @author 于谦涛
 * @since 2021-06-09
 */
@RestController
@RequestMapping("/manage")
public class AdminAddWorkerControler {
    @Resource
    private WorkersService workersService;
    @Resource
    private DistrictService districtService;
    /**获得所有的员工列表
     * @author 于谦涛
     * @since 2021-06-09
     */
    @GetMapping("showWorkersList")
    public Result showList(Integer pageNum,Integer pageSize){
        if (null==pageNum||0==pageNum){
            pageNum=1;
        }
        if (null==pageSize||pageSize==0){
            pageSize=10;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Workers> workers=workersService.list();
        PageInfo<Workers> workersPageInfo=new PageInfo<>(workers);
        return Result.success("显示员工列表",workersPageInfo);
    }

    /**添加员工信息
     * @author 于谦涛
     * @since 2021-06-09
     */
    @PostMapping("addWorker")
    public Result addWorker(@RequestBody Workers worker){
        worker.setName(UUID.randomUUID().toString().substring(0,11));
        worker.setAddress("山东省"+"青岛市"+worker.getAddress());
        worker.setPass("123456");
        System.out.println(worker.toString());
        //worker.setProfile(fileName);
        workersService.save(worker);
        return Result.success("成功");
    }
    @PostMapping("uploadWorkerPicture")
    public Result upload(@RequestParam MultipartFile file)
    {
        String fileName=PictureUtils.picture(file);
        return Result.success("成功",fileName);
    }

    /**修改员工信息
     * @author 于谦涛
     * @since 2021-06-09
     */
    @PostMapping("updateWorker")
    public Result updateWorker(@RequestParam MultipartFile file,@RequestBody Workers worker){

        return null;
    }
    /**删除员工信息
     * @author 于谦涛
     * @since 2021-06-19
     */
    @GetMapping("delete")
    public Result deleteWorker(Integer id){
        Boolean flag=workersService.removeById(id);
        if (flag){
            return Result.success("删除成功");
        }
        else {
            return Result.error("删除失败");
        }
    }
    /**删除员工信息
     * @author 于谦涛
     * @since 2021-06-19
     */
    @PostMapping("delete")
    public Result deleteWorkers(@RequestParam("ids[]") Integer[] ids){
        System.out.println(ids.toString());
       // Boolean flag=workersService.removeByIds(ids);
        if (true){
            return Result.success("删除成功");
        }
        else {
            return Result.error("删除失败");
        }
    }
    /**删除员工信息
     * @author 于谦涛
     * @since 2021-06-19
     */
    @GetMapping("showDistrictList")
    public Result showDistrictList(){
        LambdaQueryWrapper<District> queryWrapper=new LambdaQueryWrapper();
        queryWrapper.eq(District::getCityId,144);
        List<District> districts=districtService.list();
        return Result.success("成功",districts);
    }
}
